from osv import fields
from osv import osv
import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record

class wiz_analytic_open(wizard.interface):
    def _open_analytic(self, cr, uid, data, context):
       # raise osv.except_osv(_('Error !'), _("There is no expense account defined for this product:\
               # Select ai.id,account_analytic_id from account_invoice ai left join account_invoice_line ail on ai.id=ail.invoice_id\
                #where account_analytic_id in (" +','.join(map(str,data['ids']))+")"))
        cr.execute("Select Distinct job_id from purchase_order po \
                    where po.id in (" +','.join(map(str,data['ids']))+")")
        po_id = cr.fetchall()
        if len(po_id)==1:
            value = {
            'domain': "[('id', 'in', "+str(po_id)+")]",
            'name': 'Projects',
            'view_type': 'form',
            'view_mode': 'form,tree',
            'res_id':po_id[0][0],
            'res_model': 'account.analytic.account',
            'view_id': False,
            'type': 'ir.actions.act_window'
                }
        else:
            value = {
            'domain': "[('id', 'in', "+str(po_id)+")]",
            'name': 'Projects',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'account.analytic.account',
            'view_id': False,
            'type': 'ir.actions.act_window'
            }        
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_analytic, 'state':'end'}
        }
    }
wiz_analytic_open('analytic.open')